package com.woniuxy.woniuhis.controller;

import java.util.List;

import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.woniuhis.entity.Illness;
import com.woniuxy.woniuhis.entity.Outpatient;
import com.woniuxy.woniuhis.mapper.OutpatientMapper;
import com.woniuxy.woniuhis.service.IllnessService;
import com.woniuxy.woniuhis.service.OutpatientService;
import com.woniuxy.woniuhis.util.ResponseJson;

@Controller
@RequestMapping("outpatient")
public class OutpatientController {
	
	@Autowired
	OutpatientService outptiantService;
	
	@Autowired
	IllnessService illnessService;

	//查询该科室未开始就诊患者
	@RequestMapping("findAllNotStartOutpatient")
	@ResponseBody
	public ResponseJson<Outpatient> findAllNotStartOutpatient(){
		ResponseJson<Outpatient> notStartRegisInfoJson = null;
		try {
			notStartRegisInfoJson = outptiantService.findAllNotStartOutpatient();
			return notStartRegisInfoJson;
		} catch (Exception e) {
			notStartRegisInfoJson.setCode(1);
			notStartRegisInfoJson.setMsg(e.getMessage());
			return notStartRegisInfoJson;
		}
	}
	
	
	//查询患者历史就诊记录
	@RequestMapping("historyOutpatientByPid")
	@ResponseBody
	public List<Outpatient> historyOutpatientByPid(@PathParam("patientId") String patientId){
		List<Outpatient> outpatientList = null;
		try {
			outpatientList = outptiantService.findAllOutpatientByPid(patientId).getData();
			return outpatientList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	//将该门诊信息的开始状态置为1
	@RequestMapping("startOutpatient")
	@ResponseBody
	public String startOutpatient(@PathParam("outpatientId") String outpatientId) {
		try {
			outptiantService.startOutpatientByOid(outpatientId);
			return "开始该患者门诊流程";
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护中";
		}
	}
	
	//保存患者的描述
	@RequestMapping("savePatientDescByOid")
	@ResponseBody
	public String savePatientDescByOid(@PathParam("outpatientId") String outpatientId,@PathParam("patientDesc") String patientDesc) {
		try {
			outptiantService.savePatientDescByOid(outpatientId,patientDesc);
			return "保存成功";
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护中";
		}
	}
	
	//获取所有疾病信息
	@RequestMapping("allIllness")
	@ResponseBody
	public ModelAndView allIllness() {
		ModelAndView mv = new ModelAndView();
		List<Illness> illnesses = illnessService.findAIllnesses();
		mv.addObject("illnesses", illnesses);
		mv.setViewName("views/outpatient/outpatientEnd");
		return mv;
	}
	
	//结束门诊
	@RequestMapping("outpatientEnd")
	@ResponseBody
	public String outpatientEnd(@RequestBody Outpatient outpatient,@PathParam("outpatientId") String outpatientId) {
		try {
			//将信息存入outpatientId门诊中
			outptiantService.outpatientEnd(outpatientId,outpatient);
			return "保存成功";
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护中";
		}
	}
	
}
